﻿using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using System.Diagnostics;
using UnityEditor;
#endif
namespace LitTools
{
    /// <summary>
    /// 持久文件工具类
    /// </summary>
    public class PresistentFileUtility
    {
#if UNITY_EDITOR
        /// <summary>
        /// 打开持久化文件路径
        /// </summary>
        [MenuItem("LitTools/持久化文件/打开路径")]
        private static void OpenPresistentFile()
        {
            Process.Start(new ProcessStartInfo() { FileName = Application.persistentDataPath });
        }
#endif

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        /// <param name="append">是否续写</param>
        public static void SaveFile(string path, string content, bool append = false)
        {
            var fullPath = Application.persistentDataPath + "/" + path;
            if (!File.Exists(fullPath))
            {
                string directoryPath = Path.GetDirectoryName(fullPath);
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);
                FileStream fs = File.Create(fullPath);
                fs.Close();
                UnityEngine.Debug.Log($"[PresistentFileUtility] SaveFile create file:{fullPath}");
            }
            StreamWriter sw = new StreamWriter(fullPath, append);
            sw.Write(content);
            sw.Close();
            UnityEngine.Debug.Log($"[PresistentFileUtility] SaveFile open file:{fullPath} content:\n {content}");
        }

        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string LoadFile(string path, string defaultValue = "")
        {
            var fullPath = Application.persistentDataPath + "/" + path;
            if (File.Exists(fullPath))
            {
                StreamReader sr = new StreamReader(fullPath);
                var content = sr.ReadToEnd();
                sr.Close();
                UnityEngine.Debug.Log($"[PresistentFileUtility] LoadFile file:{fullPath} content:\n {content}");
                return content;
            }
            else
            {
                UnityEngine.Debug.LogWarning($"[PresistentFileUtility] LoadFile file:{fullPath} not exist");
                return defaultValue;
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteFile(string path)
        {
            string fullPath = Application.persistentDataPath + "/" + path;
            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
                UnityEngine.Debug.Log($"[PresistentFileUtility] DeleteFile file:{fullPath}");
            }
            else
            {
                UnityEngine.Debug.LogWarning($"[PresistentFileUtility] DeleteFile file:{fullPath} not exist");
            }
        }

#if UNITY_EDITOR
        /// <summary>
        /// 删除所有文件
        /// </summary>
        [MenuItem("LitTools/持久化文件/删除所有")]
#endif
        public static void DeleteAllFile()
        {
            if (Directory.Exists(Application.persistentDataPath))
            {
                Directory.Delete(Application.persistentDataPath, true);
                UnityEngine.Debug.Log($"[PresistentFileUtility] DeleteAllFile file");
            }
            else
            {
                UnityEngine.Debug.LogWarning($"[PresistentFileUtility] DeleteAllFile file not exist");
            }
        }
    }
}